/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-03-08     Hao       the first version
 */
#include <hardware/gpio_operate.h>
#include "drv_common.h"

// 驱动芯片使能引脚
gpio_t enable_pin =
{
    .pin = GET_PIN(B, 1),
    .mode = PIN_MODE_OUTPUT,
};

// SPI2片选引脚
gpio_t spi2_cs_pin =
{
    .pin = GET_PIN(B, 12),
    .mode = PIN_MODE_OUTPUT,
};

// CAN地址1
gpio_t can_addr1_pin =
{
    .pin = GET_PIN(A, 3),
    .mode = PIN_MODE_INPUT,
};

// CAN地址2
gpio_t can_addr2_pin =
{
    .pin = GET_PIN(A, 4),
    .mode = PIN_MODE_INPUT,
};

int gpio_operate_init(void)
{
    rt_pin_mode(enable_pin.pin, enable_pin.mode);
    rt_pin_mode(spi2_cs_pin.pin, spi2_cs_pin.mode);
    rt_pin_mode(can_addr1_pin.pin, can_addr1_pin.mode);
    rt_pin_mode(can_addr2_pin.pin, can_addr2_pin.mode);

    rt_pin_write(enable_pin.pin, 1);
    return 0;
}
INIT_APP_EXPORT(gpio_operate_init);

void gpio_write(gpio_ptr port_ptr, rt_base_t value)
{
    rt_pin_write(port_ptr->pin, value);
}

uint8_t gpio_read(gpio_ptr port_ptr)
{
    return (uint8_t)rt_pin_read(port_ptr->pin);
}

#if 0

void gpio_test()
{
    for(uint8_t i = 0; i <= 10; i++)
    {
        gpio_write(&enable_pin, 1);
        rt_thread_mdelay(1000);
        gpio_write(&enable_pin, 0);
        rt_thread_mdelay(1000);
    }
}
MSH_CMD_EXPORT(gpio_test, gpio test);

#endif
